import { cloneDeep, } from 'lodash-es';

let configList: ConfigRecord[] = [
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": "",
    "updateTime": null,
    "remark": "蓝色 skin-blue、绿色 skin-green、紫色 skin-purple、红色 skin-red、黄色 skin-yellow",
    "configId": 1,
    "configName": "主框架页-默认皮肤样式名称",
    "configKey": "sys.index.skinName",
    "configValue": "skin-blue",
    "configType": "Y"
  },
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": "",
    "updateTime": null,
    "remark": "初始化密码 123456",
    "configId": 2,
    "configName": "用户管理-账号初始密码",
    "configKey": "sys.user.initPassword",
    "configValue": "123456",
    "configType": "Y"
  },
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": "",
    "updateTime": null,
    "remark": "深色主题theme-dark，浅色主题theme-light",
    "configId": 3,
    "configName": "主框架页-侧边栏主题",
    "configKey": "sys.index.sideTheme",
    "configValue": "theme-dark",
    "configType": "Y"
  },
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": "",
    "updateTime": null,
    "remark": "是否开启验证码功能（true开启，false关闭）",
    "configId": 4,
    "configName": "账号自助-验证码开关",
    "configKey": "sys.account.captchaEnabled",
    "configValue": "true",
    "configType": "Y"
  },
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": "",
    "updateTime": null,
    "remark": "是否开启注册用户功能（true开启，false关闭）",
    "configId": 5,
    "configName": "账号自助-是否开启用户注册功能",
    "configKey": "sys.account.registerUser",
    "configValue": "false",
    "configType": "Y"
  },
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": "",
    "updateTime": null,
    "remark": "设置登录IP黑名单限制，多个匹配项以;分隔，支持匹配（*通配、网段）",
    "configId": 6,
    "configName": "用户登录-黑名单列表",
    "configKey": "sys.login.blackIPList",
    "configValue": "",
    "configType": "Y"
  }
];

const mocks: MockRecord[] = [
  {
    method: "get",
    url: "/api/system/config/list",
    response(option) {
      const copyList = cloneDeep(configList);

      let resList = configList;

      if (option.query.pageNum) {
        resList = copyList.splice((option.query.pageNum - 1) * option.query.pageSize, option.query.pageSize);
      }

      const resData: responseType = {
        code: 200,
        msg: "查询成功",
        total: configList.length,
        ...(option.query.pageNum ? { rows: resList } : { data: resList })
      }

      return resData;
    }
  },
  {
    method: "get",
    url: "/api/system/config/:configId",
    response(option) {
      const resConfig = configList.find(item => item.configId == option.query.configId);

      const resData: responseType = {
        code: resConfig ? 200 : 500,
        msg: resConfig ? "查询成功" : "查询失败",
        data: resConfig,
      }

      return resData;
    }
  },
  {
    method: "post",
    url: "/api/system/config",
    response(option) {
      const newConfig = option.body;

      newConfig.configId = configList.length + 1;
      configList.unshift(newConfig);

      const resData: responseType = {
        code: 200,
        msg: "新增成功",
      }

      return resData;
    }
  },
  {
    method: "put",
    url: "/api/system/config",
    response(option) {
      const newConfig = option.body;

      const index = configList.findIndex(item => item.configId == newConfig.configId);

      configList.splice(index, 1, newConfig);

      const resData: responseType = {
        code: 200,
        msg: "修改成功",
      }

      return resData;
    }
  },
  {
    method: "delete",
    url: "/api/system/config/:configIds",
    response(option) {
      const configIds = option.query.configIds.split(',');

      const filterList = configList.filter(item => !configIds.includes(String(item.configId)));

      configList = filterList;

      const resData: responseType = {
        code: 200,
        msg: "删除成功",
      }
      return resData;
    }
  }
];

export default mocks;